package com.moo2u2.android.oaktononthego.crm.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CustomerDbAdapter {

	private Context context;
	private SQLiteDatabase db;
	private CustomerDatabaseHelper dbHelper;

	public CustomerDbAdapter(Context context) {
		this.context = context;
	}

	public CustomerDbAdapter open() throws SQLException {
		dbHelper = new CustomerDatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public long createCustomer(String name, String contact, String phone,
			String fax, String email, String address, Integer wins,
			Integer losses, String description, String notes) {
		Log.i(this.getClass().getSimpleName(), "Creating customer...");
		ContentValues values = createContentValues(name, contact, phone, fax,
				email, address, wins, losses, description, notes);

		return db.insert(CustomerTable.DB_TABLE, null, values);
	}

	public boolean updateCustomer(long rowId, String name, String contact,
			String phone, String fax, String email, String address,
			Integer wins, Integer losses, String description, String notes) {
		Log.i(this.getClass().getSimpleName(), "Updating customer " + rowId
				+ "...");
		ContentValues values = createContentValues(name, contact, phone, fax,
				email, address, wins, losses, description, notes);

		return db.update(CustomerTable.DB_TABLE, values,
				CustomerTable.KEY_ROWID + "=" + rowId, null) > 0;
	}

	public long addOpportunity(Long rowId, String value) {
		Log.i(this.getClass().getSimpleName(), "Adding opportunity for "
				+ rowId + "...");
		ContentValues values = createOpportunityValues(rowId, value);

		return db.insert(OpportunityTable.DB_TABLE, null, values);
	}

	public boolean deleteCustomer(long rowId) {
		Log.i(this.getClass().getSimpleName(), "Deleting customer " + rowId
				+ "...");
		return db.delete(CustomerTable.DB_TABLE, CustomerTable.KEY_ROWID + "="
				+ rowId, null) > 0;
	}

	public boolean deleteOpportunity(long rowId) {
		Log.i(this.getClass().getSimpleName(), "Deleting opportunity " + rowId
				+ "...");
		return db.delete(OpportunityTable.DB_TABLE, OpportunityTable.KEY_ROWID
				+ "=" + rowId, null) > 0;
	}

	public Cursor fetchAllCustomers() {
		return db.query(CustomerTable.DB_TABLE, new String[] {
				CustomerTable.KEY_ROWID, CustomerTable.KEY_NAME,
				CustomerTable.KEY_PHONE, CustomerTable.KEY_FAX,
				CustomerTable.KEY_EMAIL, CustomerTable.KEY_ADDRESS,
				CustomerTable.KEY_WINS, CustomerTable.KEY_LOSSES,
				CustomerTable.KEY_DESCRIPTION, CustomerTable.KEY_NOTES }, null,
				null, null, null, null, null);
	}

	public Cursor fetchCustomer(long rowId) throws SQLException {
		Cursor mCursor = db.query(true, CustomerTable.DB_TABLE, new String[] {
				CustomerTable.KEY_ROWID, CustomerTable.KEY_NAME,
				CustomerTable.KEY_CONTACT, CustomerTable.KEY_PHONE,
				CustomerTable.KEY_FAX, CustomerTable.KEY_EMAIL,
				CustomerTable.KEY_ADDRESS, CustomerTable.KEY_WINS,
				CustomerTable.KEY_LOSSES, CustomerTable.KEY_DESCRIPTION,
				CustomerTable.KEY_NOTES }, CustomerTable.KEY_ROWID + "="
				+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor fetchOpportunities(Long mRowId) {
		Cursor mCursor = db.query(true, OpportunityTable.DB_TABLE,
				new String[] { OpportunityTable.KEY_ROWID,
						OpportunityTable.KEY_DESC },
				OpportunityTable.KEY_CUSTOMER + "=" + mRowId, null, null, null,
				null, null);
		if (mCursor != null) {
			if (!mCursor.moveToFirst())
				return null;
		}
		return mCursor;
	}

	private ContentValues createContentValues(String name, String contact,
			String phone, String fax, String email, String address,
			Integer wins, Integer losses, String description, String notes) {
		ContentValues values = new ContentValues();
		values.put(CustomerTable.KEY_NAME, name);
		values.put(CustomerTable.KEY_CONTACT, contact);
		values.put(CustomerTable.KEY_PHONE, phone);
		values.put(CustomerTable.KEY_FAX, fax);
		values.put(CustomerTable.KEY_EMAIL, email);
		values.put(CustomerTable.KEY_ADDRESS, address);
		values.put(CustomerTable.KEY_WINS, wins);
		values.put(CustomerTable.KEY_LOSSES, losses);
		values.put(CustomerTable.KEY_DESCRIPTION, description);
		values.put(CustomerTable.KEY_NOTES, notes);
		return values;
	}

	private ContentValues createOpportunityValues(long id, String value) {
		ContentValues values = new ContentValues();
		values.put(OpportunityTable.KEY_CUSTOMER, id);
		values.put(OpportunityTable.KEY_DESC, value);
		return values;
	}

	public boolean hasData() {
		Cursor customers = fetchAllCustomers();
		if (customers == null || !customers.moveToFirst())
			return false;
		else
			return true;
	}

}